let MS = require("./ms.js")
const request = require("./request.js")
const Error = require('./error')
const storage = require("./storage.js")
const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType')

const order = class order {
    constructor() {
    }

    create(shopBagIds) {
        let data = {
            userId : MS.User.current().id,
            userName: MS.User.current().nickName,
            shopBagIds: shopBagIds
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.CREATE_ORDER.url,
                MS._config.parameters.SHOP.CREATE_ORDER.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    get(orderId) {
        let data = {orderId: orderId};
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.GET_ORDER.url,
                MS._config.parameters.SHOP.GET_ORDER.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    paySuccess(options) {
        var params = {}
        if(!isUndefined(options.id) && isString(options.id)) {
            params.orderId = options.id;
        }
        if(!isUndefined(options.cityName) && isString(options.cityName)) {
            params.cityName = options.cityName;
        }
        if(!isUndefined(options.pickUpName) && isString(options.pickUpName)) {
            params.pickUpName = options.pickUpName;
        }
        if(!isUndefined(options.pickUpPhone) && isString(options.pickUpPhone)) {
            params.pickUpPhone = options.pickUpPhone;
        }
        if(!isUndefined(options.pickUpAddress) && isString(options.pickUpAddress)) {
            params.pickUpAddress = options.pickUpAddress;
        }
        if(!isUndefined(options.wxOrderId) && isString(options.wxOrderId)) {
            params.wxOrderId = options.wxOrderId;
        }
        if(!isUndefined(options.payIntegral) && isNumber(options.payIntegral)) {
            params.payIntegral = options.payIntegral;
        }
        if(!isUndefined(options.payPrice) && isNumber(options.payPrice)) {
            params.payPrice = options.payPrice;
        }
        if(!isUndefined(options.courierPrice) && isNumber(options.courierPrice)) {
            params.courierPrice = options.courierPrice;
        }
        if(!isUndefined(options.isRebate) && isBoolean(options.isRebate)) {
            params.isRebate = options.isRebate;
        }
        if(!isUndefined(options.totalShareProfit) && isNumber(options.totalShareProfit)) {
            params.totalShareProfit = options.totalShareProfit;
        }
        if(!isUndefined(options.refereeId) && isString(options.refereeId)) {
            params.refereeId = options.refereeId;
        }
        if(!isUndefined(options.refereeName) && isString(options.refereeName)) {
            params.refereeName = options.refereeName;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.PAY_SUCCESS.url,
                MS._config.parameters.SHOP.PAY_SUCCESS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    cancel(orderId) {
        let data = {orderId: orderId};
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.CREATE_ORDER.url,
                MS._config.parameters.SHOP.CREATE_ORDER.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            userId: MS.User.current().id
        };
        if(!isUndefined(options.userId) && isString(options.userId)) {
            params.userId = options.userId;
        }
        if(!isUndefined(options.wxOrderId) && isString(options.wxOrderId)) {
            params.wxOrderId = options.wxOrderId;
        }
        if(!isUndefined(options.ice) && isBoolean(options.ice)) {
            params.ice = options.ice;
        }
        if(!isUndefined(options.status) && isBoolean(options.status)) {
            params.status = options.status;
        }
        if(!isUndefined(options.isRebate) && isBoolean(options.isRebate)) {
            params.isRebate = options.isRebate;
        }
        if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            params.pageSize = options.pageSize;
        }
        if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            params.pageNum = options.pageNum;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.FIND_ORDER.url,
                MS._config.parameters.SHOP.FIND_ORDER.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    findOrderExpress(orderId) {
        let params = {
            orderId: orderId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.FIND_ORDER_EXPORESS.url,
                MS._config.parameters.SHOP.FIND_ORDER_EXPORESS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = order